package risk.control.application.activity;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.lifecycle.LifecycleOwner;

import com.otaliastudios.cameraview.PictureResult;

import java.util.HashMap;

import risk.control.application.R;
import risk.control.application.activity.base.BaseActivity;

import risk.control.application.component.msg.ConfirmClickListener;
import risk.control.application.component.msg.ShowMsgDialog;
import risk.control.application.component.pwd.ChangePwdClickListener;
import risk.control.application.component.pwd.ChangePwdDialog;
import risk.control.application.http.Api;
import risk.control.application.http.HttpPostClient;
import risk.control.application.http.callback.UiCallback;
import risk.control.application.utils.DeviceUtils;
import risk.control.application.utils.RouteJump;
import risk.control.application.utils.ToastUtils;
import risk.control.application.utils.data.ActivityMapUtils;
import risk.control.application.utils.data.TempCacheUtils;
import risk.control.application.utils.data.TokenUtils;
import risk.control.application.utils.mmkv.MMKVUtils;
import risk.control.application.utils.mmkv.MMKV_KEY;


public class MoreActivity extends BaseActivity implements LifecycleOwner {

    private TextView mTvTitle;
    private Button mBtnLogout;

    private Button mBtnChangePwd;

    private TextView mTvOs;
    private TextView mTvVersion;

    @Override
    protected void postData(HashMap<String, String> dataMap) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        intiView();
        intiData();
        initListener();
    }


    private void intiView() {
        mTvTitle = findViewById(R.id.tv_result_title);
        mBtnLogout = findViewById(R.id.btn_logout);

        mTvOs = findViewById(R.id.tv_os);
        mTvVersion = findViewById(R.id.tv_version);

        mBtnChangePwd = findViewById(R.id.btn_change_pwd);
    }

    @SuppressLint("SetTextI18n")
    private void intiData() {
        mTvTitle.setText("更多页面");
        mTvOs.setText("操作系统：Android" + DeviceUtils.getOsVersion() + "（api" + DeviceUtils.getApiLevel() + "）");
        mTvVersion.setText("程序版本：V" + DeviceUtils.getAppVersion());
    }


    private void initListener() {
        mBtnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowMsgDialog dialog = new ShowMsgDialog(MoreActivity.this);
                dialog.serTitle("注销");
                dialog.setBtnText("确认");
                dialog.setCancelable(true);
                dialog.setBtnClickListener(new ConfirmClickListener() {
                    @Override
                    public void onPositiveClick() {
                        logout();
                    }
                });
                dialog.showDialog("是否确认注销登录？");
            }
        });

        mBtnChangePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChangePwdDialog changePwdDialog = new ChangePwdDialog(MoreActivity.this);
                changePwdDialog.showDialog();
                changePwdDialog.setBtnClickListener(new ChangePwdClickListener() {
                    @Override
                    public void onPositiveClick(String currencyType) {
                        changePwd(currencyType);
                    }
                });
            }
        });
    }

    private void changePwd(String newPwd) {
        showLoadDialog("正在修改密码...");
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", TokenUtils.get());
        paramsMap.put("newPwd", newPwd);
        getNetWordData(Api.changPwd, paramsMap, new UiCallback() {
            @Override
            public void onSuccess(String respData) {
                ToastUtils.showShort(MoreActivity.this, "密码修改成功需重新登录");
                dismissLoadDialog();
                logout();
            }

            @Override
            public void onSpecialCode(String errorData) {
                dismissLoadDialog();
            }

            @Override
            public void onResolve() {
                dismissLoadDialog();
            }
        });
    }

    private void logout() {
        showLoadDialog("正在注销...");
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", TokenUtils.get());
        getNetWordData(Api.logOut, paramsMap, new UiCallback() {
            @Override
            public void onSuccess(String respData) {
                //关闭所有activity,清空用户数据-跳转登录页面
                dismissLoadDialog();
                MMKVUtils.remove(MMKV_KEY.KV_USER_INFO_DATA);//token
                TempCacheUtils.getInstance().clearAll();
                HttpPostClient.getInstance().cancelAll();
                ActivityMapUtils.finishAll();
                RouteJump.start(MoreActivity.this, RouteJump.LOGIN_ACTION, null, true, true);
            }

            @Override
            public void onSpecialCode(String errorData) {
                dismissLoadDialog();
            }

            @Override
            public void onResolve() {
                dismissLoadDialog();
            }
        });
    }

    //处理图片
    private void handleResult(PictureResult result) {

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();


    }

}





